/**
 * Created by chengming on 18/5/27.
 * version 1.0
 */
const ArticleHrefAdapter = require('../ArticleHrefAdapter');
const iconv = require("iconv-lite");
const vm = require('vm');
const request = require('request-promise');
const logger = require('../../conf/logger');
const querystring = require("querystring")
const urlencode = require("urlencode")
const cheerio = require('cheerio');
/**
 * 北京互金网
 * @type {module.Bjp2p}
 */
module.exports = class SenZenGovJs extends ArticleHrefAdapter{

    initWinEnv(){
        global.window = {
            innerWidth:1232,
            innerHeight:632,
            screenX:11,
            screenY:22,
            screen:{
                width:1280,
                height:800
            }
        };

        global.document = {
            documentElement:{
                clientWidth:1217,
                clientHeight:550
            },
            body:{
                clientWidth:1217,
                clientHeight:1467
            },
            _cookie: [],

            get cookie(){
                return this._cookie;
            },

            set cookie(val){
                this._cookie.push(val);
            }
        };
    }

    parseHtml(html, linkRule){
        //将解析后的内容加载进解析器
        let $ = cheerio.load(html);

        let contents = [];
        let that = this;
        let rangeText = '';
        $(this.linkRule).each( function() {
            let itemHtml = $(this).html();
            let $$ = cheerio.load(itemHtml);
            let jsStr = $$('script').html();
            jsStr = jsStr.substring(0,jsStr.indexOf('if(_type'));
            console.log('----jsStr: ' + jsStr)
            vm.runInThisContext(jsStr);

            if(title) {
                title = urlencode.decode(title,'utf-8')
                console.log('----_url: ' + _url)
                let href = 'http://www.sz.gov.cn/zfbgt/gzwj/gz_1/' + _url.replace(/\.\//g,'');

                if(title.length != 0 || href.length != 0){
                    contents.push({
                        title:title,
                        href:href
                    });
                    rangeText +=title+href;
                }
            }

        });

        return {
            rangeText: rangeText,
            contents: contents
        };
    }


}
